import apis from './api'
import { Random, setup, mock } from 'mockjs'
import { range } from 'lodash-es'

setup({
  timeout: '200-1000',
})

const phonePrefixs = [
  133, 153, 180, 181, 189, 177, 173, 149, 130, 131, 132, 155, 156, 145, 185, 186, 176, 175, 135, 136, 137, 138, 139,
  150, 151, 152, 157, 158, 159, 182, 183, 184, 187, 188, 147, 178,
]

Random.extend({
  phone: function () {
    return `${this.pick(phonePrefixs)}****${mock(/\d{4}/)}`
  },
  menu: function (min = 5, max = 15) {
    return range(mock(`@integer(${min},${max})`)).map((e) => {
      const menu = mock({
        id: '@id',
        belong: 'root', // 归属于
        name: '@cword(5,7)',
        'type|1': ['sub-menu', 'menu-item'], // 目录||页面
        path: `/${mock('@word')}/${mock('@word')}`, // 路径
        'status|1': [1, 1, 1, 1, 1, 1, 1, 1, 0],
        icon: 'ep:menu',
        remark: '@csentence(25)',
      })
      if (Math.random() > 0.7) {
        const children = mock('@menu(0,4)')
        return {
          ...menu,
          children,
        }
      }
      return menu
    })
  },
})

const mocks = [...apis]

export interface IMockApi {
  url: string | RegExp
  method: 'get' | 'post' | 'delete' | 'put'
  data: {
    code: 200 | 500 | 404 | 403
    data?: Record<any, any>
    msg?: string
  }
}

mocks.map((e) => {
  let exp = e.url
  if (!(exp instanceof RegExp)) exp = new RegExp(e.url)
  mock(exp, e.method, e.data)
})
